package com.practice.system.service.impl;

import com.practice.system.dto.PlanningDetailDto;
import com.practice.system.mapper.LinkPlanningDetailchedulingMapper;
import com.practice.system.mapper.PlanningDetailMapper;
import com.practice.system.pojo.PlanningDetail;
import com.practice.system.service.PlanningDetailService;
import com.practice.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class PlanningDetailServiceImpl implements PlanningDetailService {
    @Autowired
    LinkPlanningDetailchedulingMapper linkPlanningDetailchedulingMapper;
    @Autowired
    PlanningDetailMapper planningDetailMapper;
    @Override
    public R listbyschedulingid(String id) {
        List<PlanningDetail> MaterielListResult = new ArrayList<>();
        List<String> MaterielList = linkPlanningDetailchedulingMapper.listByPlanningChedulingId(id);
        //拿到下半部分详情表的list，进行查询物料表，并绑定到MaterielListResult
        System.out.println(MaterielList);
        for (String I : MaterielList){
            MaterielListResult.add(planningDetailMapper.findById(I));
        }
        return R.ok().put("page",MaterielListResult);
    }

    @Override
    public R update(PlanningDetail planningDetail) {
        if(planningDetailMapper.update(planningDetail))
            return R.ok();
        else
            return R.error();
    }

    @Override
    public R add(PlanningDetailDto planningDetailDto) {
        planningDetailDto.setId(UUID.randomUUID().toString());
        //新增详情表数据
        planningDetailMapper.add(planningDetailDto);
        //新增关联表数据
        linkPlanningDetailchedulingMapper.addByTwoId(planningDetailDto.getScheduling(),planningDetailDto.getId());
        return R.ok();
    }
}
